/*
day241
2020.4.25
*/ 
#include<iostream>
#include<vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    TreeNode* bstFromPreorder(vector<int>& preorder) {
		return help(preorder,0,preorder.size()-1);
    }
    
    TreeNode* help(vector<int>& preorder,int left,int right)
    {
    	if(left>right)
    	{
    		return nullptr;
		}
    	TreeNode *root=new TreeNode(preorder[left]);
    	
    	int tem=left;
    	while(tem<preorder.size() && preorder[tem]<=preorder[left])
    	{
    		++tem;
		}
		
		root->left=help(preorder,left+1,tem-1);
		root->right=help(preorder,tem,right);
		
		return root; 
	}
};


int main()
{
	return 0;
}
